package htwt

import (
	"fmt"
	"log"
	"time"
)

// BaseOperator
type BaseOperator struct {
	htwtWnd *HtwtWnd
}

// ConnectHtwt 链节下单客户端
func (baseOperator *BaseOperator) ConnectClient() error {
	mainWnd := NewHtwtWnd()
	err := mainWnd.UpdateWindowHandle()
	if err != nil {
		return fmt.Errorf("mainWnd.UpdateWindowHandle failed, %v", err)
	}
	mainWnd.CloseUnimportancePopWindow()
	baseOperator.htwtWnd = mainWnd
	return nil
}

func (baseOperator BaseOperator) checkAccount(account string) error {
	mainWnd := baseOperator.htwtWnd
	name, err := mainWnd.statusWnd.GetAccountName()
	if err != nil {
		return fmt.Errorf("mainWnd.statusWnd.GetAccountName failed, %v", err)
	}
	if account != name {
		return fmt.Errorf("account name mismatch, %s != %s", account, name)
	}
	return nil
}

// Buy 买入操作
func (baseOperator BaseOperator) BuyNumber(account string, ticker string, number uint64) (float64, error) {
	err := baseOperator.checkAccount(account)
	if err != nil {
		return 0, fmt.Errorf("baseOperator.checkAccount failed, %v", err)
	}

	mainWnd := baseOperator.htwtWnd
	log.Println(mainWnd)
	mainWnd.workWnd.buyWnd.Show()
	time.Sleep(time.Second)

	ticker = ticker[2:]

	err = mainWnd.workWnd.buyWnd.FillStockCode(ticker)
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.buyWnd.FillStockCode failed, %v", err)
	}
	time.Sleep(time.Second)

	// info2, err := mainWnd.workWnd.buyWnd.tenPriceWnd.Info()
	// if err != nil {
	// 	return 0, 0, fmt.Errorf("mainWnd.workWnd.buyWnd.tenPriceWnd.Info failed, %v", err)
	// }

	price, amount, err := mainWnd.workWnd.buyWnd.tenPriceWnd.Sale1()
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.buyWnd.tenPriceWnd.Sale1 failed, %v", err)
	}

	if price <= 0 {
		return 0, fmt.Errorf("price <= 0")
	}

	if amount <= 0 {
		return 0, fmt.Errorf("amount <= 0")
	}

	count := number // uint64(money / price)
	// count = count / 100
	// count = count * 100
	// if count < 100 {
	// 	return 0, 0, fmt.Errorf("买入数量 < 100 股")
	// }

	log.Printf("买入 %s, 价格: %f, 数量: %d", ticker, price, count)

	ContractNo, err := mainWnd.workWnd.buyWnd.Buy(ticker, price, count)
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.buyWnd.Buy failed, %v", err)
	}
	log.Printf("买入已提交, 合同编号ID: %d", ContractNo)

	return price * float64(count), nil
}

// Buy 买入操作
func (baseOperator BaseOperator) BuyMoney(account string, ticker string, money float64) (int, float64, error) {
	err := baseOperator.checkAccount(account)
	if err != nil {
		return 0, 0, fmt.Errorf("baseOperator.checkAccount failed, %v", err)
	}

	mainWnd := baseOperator.htwtWnd
	log.Println(mainWnd)
	mainWnd.workWnd.buyWnd.Show()
	time.Sleep(time.Second)

	ticker = ticker[2:]

	err = mainWnd.workWnd.buyWnd.FillStockCode(ticker)
	if err != nil {
		return 0, 0, fmt.Errorf("mainWnd.workWnd.buyWnd.FillStockCode failed, %v", err)
	}
	time.Sleep(time.Second)

	// info2, err := mainWnd.workWnd.buyWnd.tenPriceWnd.Info()
	// if err != nil {
	// 	return 0, 0, fmt.Errorf("mainWnd.workWnd.buyWnd.tenPriceWnd.Info failed, %v", err)
	// }

	price, amount, err := mainWnd.workWnd.buyWnd.tenPriceWnd.Sale1()
	if err != nil {
		return 0, 0, fmt.Errorf("mainWnd.workWnd.buyWnd.tenPriceWnd.Sale1 failed, %v", err)
	}

	if price <= 0 {
		return 0, 0, fmt.Errorf("price <= 0")
	}

	if amount <= 0 {
		return 0, 0, fmt.Errorf("amount <= 0")
	}

	count := uint64(money / price)
	count = count / 100
	count = count * 100
	if count < 100 {
		return 0, 0, fmt.Errorf("买入数量 < 100 股")
	}

	log.Printf("买入 %s, 价格: %f, 数量: %d", ticker, price, count)

	ContractNo, err := mainWnd.workWnd.buyWnd.Buy(ticker, price, count)
	if err != nil {
		return 0, 0, fmt.Errorf("mainWnd.workWnd.buyWnd.Buy failed, %v", err)
	}
	log.Printf("买入已提交, 合同编号ID: %d", ContractNo)

	return int(count), price * float64(count), nil
}

// Sale 卖出操作
func (baseOperator BaseOperator) Sale(account string, ticker string, count uint64) (float64, error) {
	err := baseOperator.checkAccount(account)
	if err != nil {
		return 0, fmt.Errorf("baseOperator.checkAccount failed, %v", err)
	}

	mainWnd := baseOperator.htwtWnd
	mainWnd.workWnd.saleWnd.Show()
	time.Sleep(time.Second)

	ticker = ticker[2:]

	err = mainWnd.workWnd.saleWnd.FillStockCode(ticker)
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.saleWnd.FillStockCode failed, %v", err)
	}
	time.Sleep(time.Second)

	// info2, err := mainWnd.workWnd.saleWnd.tenPriceWnd.Info()
	// if err != nil {
	// 	return 0, fmt.Errorf("mainWnd.workWnd.saleWnd.tenPriceWnd.Info failed, %v", err)
	// }

	var getBuyPriceFunc []func() (price float64, amount uint64, err error)
	// getBuyPriceFunc = append(getBuyPriceFunc, mainWnd.workWnd.saleWnd.tenPriceWnd.Buy4)
	// getBuyPriceFunc = append(getBuyPriceFunc, mainWnd.workWnd.saleWnd.tenPriceWnd.Buy3)
	// getBuyPriceFunc = append(getBuyPriceFunc, mainWnd.workWnd.saleWnd.tenPriceWnd.Buy2)
	getBuyPriceFunc = append(getBuyPriceFunc, mainWnd.workWnd.saleWnd.tenPriceWnd.Buy1)
	var price float64
	var amount uint64
	err = nil
	for i := 0; i < len(getBuyPriceFunc); i++ {
		price, amount, err = getBuyPriceFunc[i]() // mainWnd.workWnd.saleWnd.tenPriceWnd.Buy1()
		if err == nil {
			break
		}
		err = fmt.Errorf("mainWnd.workWnd.saleWnd.tenPriceWnd.Info failed, %v", err)
	}
	// price, amount, err := mainWnd.workWnd.saleWnd.tenPriceWnd.Buy1()
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.saleWnd.tenPriceWnd.Info failed, %v", err)
	}

	if price <= 0 {
		return 0, fmt.Errorf("price <= 0")
	}

	if amount <= 0 {
		return 0, fmt.Errorf("amount <= 0")
	}

	log.Printf("卖出 %s, 价格: %f, 数量: %d", ticker, price, count)

	ContractNo, err := mainWnd.workWnd.saleWnd.Sale(ticker, price, count)
	if err != nil {
		return 0, fmt.Errorf("mainWnd.workWnd.saleWnd.Sale failed, %v", err)
	}
	log.Printf("卖出已提交, 合同编号ID: %d", ContractNo)

	return price * float64(count), nil
}
